import { withLoading } from 'utils/dva';
import services from 'services';
import model from '../model';
import { pageConfig } from 'configs/constants';

const defaultConfig = {
  pagination: {
    ...pageConfig,
    total: 0
  },
  list: []
};
const initDetails = {
  company: {},
  role: {},
  user: {}
};
const initSearch = {
  eqId: '',
  dataTemplateItemId: '',
  except: ''
};


export default model.extend ({

  namespace: 'equipData',
  state: {
    search: initSearch,
    config: defaultConfig,
    user: [],
    details: initDetails,
    monitorData: {},
    equipList: [],
    templateList: [],
    initDetails,
    loading: {
      list: false,
      add: false,
      del: false
    }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      // 监听路由的变化，请求页面数据
      return history.listen(({ pathname }) => {
      });
    }
  },

  effects: {
    * getEquipList({ payload }, { call, select, update }) {
      // const { config: { pagination } } = yield select(({ equipData }) => equipData);
      // const { pageSize, current } = pagination;
      const { data } = yield call(withLoading(services.equipment.getEquipmentList), payload);
      yield update({ equipList: data.data });
    },
    * getList({ payload }, { call, select, update }) {
      const { config: { pagination }, search } = yield select(({ equipData }) => equipData);
      const { pageSize, current } = pagination;
      const { data }  = yield call(withLoading(services.chart.getMonitorDataPage, 'list'), 
      { ...search, ps: pageSize, pn: current, ...payload });
      yield update({ 
        config: {
          list: data.data,
          pagination: {
            ...pagination,
            total: data.total
          }
        }
       });
    },
    * queryDataTemplateItems({ payload }, { call, update }) {
      const { data } = yield call(withLoading(services.chart.queryDataTemplateItems), { ...payload });
      yield update({ templateList: data });
    }
  },

  reducers: {
    goPage(state, { payload }) {
      return {
        ...state,
        config: {
          ...state.config,
          pagination: {
            ...state.config.pagination,
            current: payload
          }
        }
      };
    },
    updateSearch(state, { payload: params }) {
      const { pn = 1 } = params;
      return {
        ...state,
        search: { ...state.search, ...params },
        config: {
          ...state.config,
          pagination: { ...state.config.pagination, current: pn }
        }
      };
    }
  }

});
